package com.nextbusmn.server.JDOWrappers;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import com.nextbusmn.server.dataobjects.NextBusData;
import com.nextbusmn.server.dataobjects.ScheduleDate;

public class ScheduleDateJDOWrapper {
	
	public static Date getCurrentScheduleDate() {
		PersistenceManager pm = 
				JDOWrapper.getPersistenceManagerFactory().getPersistenceManager();
		Query query = pm.newQuery(ScheduleDate.class);
		
		List<ScheduleDate> date = null;
		
		try {
			date = (List<ScheduleDate>) query.execute();
		} finally {
			query.closeAll();
		}
		
		if (date != null && date.size() != 0) {
			return date.get(0).getDate();
		} else {
			return null;
		}
	}
	
	public static void setCurrentScheduleDate(Date date) {
		
		PersistenceManager pm = 
				JDOWrapper.getPersistenceManagerFactory().getPersistenceManager();
		Query query = pm.newQuery(ScheduleDate.class);
		
		List<ScheduleDate> scheduleDate = null;
		
		try {
			scheduleDate = (List<ScheduleDate>) query.execute();
			if (scheduleDate != null && scheduleDate.size() != 0) {
				scheduleDate.get(0).setDate(date);
			} else {
				pm.makePersistent(new ScheduleDate(date));
			}
		} finally {
			query.closeAll();
		}
		
	}
	
}


